package crp.infra.test.db;

import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.ext.ScriptUtils;
import org.testcontainers.jdbc.JdbcDatabaseDelegate;

@Slf4j
public class DBCleanupCallback implements QuarkusTestBeforeEachCallback {
    @Override
    public void beforeEach(QuarkusTestMethodContext context) {
        if (context.getTestInstance().getClass().isAnnotationPresent(DBCleanup.class)) {
            PostgreSQLContainer<?> db = PostgreSQLContainerProvider.get();
            JdbcDatabaseDelegate containerDelegate = new JdbcDatabaseDelegate(db, "");
            ScriptUtils.runInitScript(containerDelegate, "db/cleanup.sql");
            LOGGER.info("database is cleaned.");
        }
    }
}
